package com.hx.study.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hx.common.utils.DateUtils;
import com.hx.common.utils.StringUtils;
import com.hx.study.domain.Paper;
import com.hx.study.domain.PaperRepository;
import com.hx.study.mapper.PaperMapper;
import com.hx.study.service.IPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 试卷Service业务层处理
 *
 * @author user
 * @date 2022-06-06
 */
@Service
public class PaperServiceImpl implements IPaperService {
    @Autowired
    private PaperMapper paperMapper;

    /**
     * 查询试卷
     *
     * @param id 试卷主键
     * @return 试卷
     */
    @Override
    public Paper selectPaperById(Integer id) {
        return paperMapper.selectPaperById(id);
    }

    /**
     * 查询试卷列表
     *
     * @param paper 试卷
     * @return 试卷
     */
    @Override
    public List<Paper> selectPaperList(Paper paper) {
        return paperMapper.selectPaperList(paper);
    }

    /**
     * 新增试卷
     *
     * @param paper 试卷
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPaper(Paper paper) {
        paper.setCreateTime(DateUtils.getNowDate());
        int rows = paperMapper.insertPaper(paper);
        insertPaperRepository(paper);
        return rows;
    }

    /**
     * 修改试卷
     *
     * @param paper 试卷
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePaper(Paper paper) {
        paper.setUpdateTime(DateUtils.getNowDate());
        paperMapper.deletePaperRepositoryByPaperId(paper.getId());
        insertPaperRepository(paper);
        return paperMapper.updatePaper(paper);
    }

    /**
     * 修改过期的试卷状态
     *
     * @param oldStatus 1
     * @param newStatus 2
     */
    public void updatePaperStatus(String oldStatus, String newStatus) {
        Paper paper = new Paper();
        paper.setStatus(oldStatus);
        List<Paper> papers = selectPaperList(paper);
        for (Paper p : papers) {
            if (p.getLimitTime().getTime() < new Date().getTime()) {
                p.setStatus(newStatus);
                paperMapper.updatePaper(p);
            }
        }
    }

    /**
     * 批量删除试卷
     *
     * @param ids 需要删除的试卷主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaperByIds(Integer[] ids) {
        paperMapper.deletePaperRepositoryByPaperIds(ids);
        return paperMapper.deletePaperByIds(ids);
    }

    /**
     * 删除试卷信息
     *
     * @param id 试卷主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaperById(Integer id) {
        paperMapper.deletePaperRepositoryByPaperId(id);
        return paperMapper.deletePaperById(id);
    }

    /**
     * 新增考试题库信息
     *
     * @param paper 试卷对象
     */
    public void insertPaperRepository(Paper paper) {
        List<PaperRepository> paperRepositoryList = paper.getPaperRepositoryList();
        Integer id = paper.getId();
        if (StringUtils.isNotNull(paperRepositoryList)) {
            List<PaperRepository> list = new ArrayList<PaperRepository>();
            for (PaperRepository paperRepository : paperRepositoryList) {
                paperRepository.setPaperId(id);
                list.add(paperRepository);
            }
            if (list.size() > 0) {
                paperMapper.batchPaperRepository(list);
            }
        }
    }
}
